package com.ciwong.epaper.util;

import android.util.Log;

import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.util.EpaperSys;
import com.ciwong.epaper.modules.evaluate.util.SpeechUtils;
import com.ciwong.mobilelib.i.BaseCallBack;

/**
 * Created by Administrator on 2017/5/4.
 */
public class ScoreUtils {


    public static String getScoreDisFormat(float scroe) {
        Log.d("scoreformat", "#########scroe######" + scroe);

        String score = String.valueOf(scroe);
//        String item_my_work_child_score = submitEvent.getScore();
        //保留2位小数
        score = PayUtil.radixPoint(Double.valueOf(score), 2);

        while (score.endsWith("0") || score.endsWith(".")) {
            if (score.endsWith("0") && (score.length() - 2) >= 0) {

                score = score.substring(0, score.length() - 1);

            } else if (score.endsWith(".") && (score.length() - 2) >= 0) {

                score = score.substring(0, score.length() - 1);
                break;
            }
        }

        Log.d("scoreformat", "#########return scroe######" + scroe);

        return score;
    }

    /**
     * 使用java正则表达式去掉小数点多余的.与0
     *
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    public static void setSocreCoefficient() {
        //先取本地的评分系数
        float scoreCoefficient = EpaperSys.getSharedFloat(EConstants.SHARE_KEY_SOCRE_COEFFICIENT, 1.0f);
        SpeechUtils.setScoreCoefficient(scoreCoefficient);
        //获取评分系数
        EpaperDao.getInstance().getScoreCoefficient(new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {

            }

            @Override
            public void failed(Object data) {

            }

            @Override
            public void success(Object data) {
                try {
                    if (data != null) {
                        float scoreCoefficient = (float) data;
                        EpaperSys.setSharedFloat(EConstants.SHARE_KEY_SOCRE_COEFFICIENT, scoreCoefficient);
                        Log.e("TAG", "获取评分系数success: " + data);
                        SpeechUtils.setScoreCoefficient(scoreCoefficient);
                    }
                } catch (Exception e) {

                }
            }
        });
    }
}
